package com.jiantong.work.impl.service.yangzhibaoxian;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.CustomerVisitDao;
import com.jiantong.work.model.yangzhibaoxian.CustomerVisit;
import com.jiantong.work.service.yangzhibaoxian.CustomerVisitService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 客户拜访
 */
@Slf4j
@Service
@RpcProvider
public class CustomerVisitServiceImpl implements CustomerVisitService {
    private CustomerVisitDao customerVisitDao;

    @Autowired
    public CustomerVisitServiceImpl(CustomerVisitDao customerVisitDao) {
        this.customerVisitDao = customerVisitDao;
    }

    @Override
    public Response<CustomerVisit> findById(Long id) {
        try {
            CustomerVisit customerVisit = (CustomerVisit) customerVisitDao.findById(id);
            if (customerVisit == null){
                log.error("customerVisit(id={}) is not found", id);
                return Response.fail("customerVisit.not.found");
            }
            return Response.ok(customerVisit);
        } catch (Exception e) {
            log.error("failed to find customerVisit(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("customerVisit.find.fail");
        }
    }

    @Override
    public Response<List<CustomerVisit>> findByIds(List ids) {
        try {
            List<CustomerVisit> customerVisitList = customerVisitDao.findByIds(ids);
            if (customerVisitList == null){
                log.error("customerVisitList(ids={}) is not found", ids);
                return Response.fail("customerVisitList.not.found");
            }
            return Response.ok(customerVisitList);
        } catch (Exception e) {
            log.error("failed to find customerVisitList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("customerVisitList.find.fail");
        }
    }

    @Override
    public Response<List<CustomerVisit>> list(Map map) {
        try {
            List<CustomerVisit> customerVisitList = customerVisitDao.list(map);
            if (customerVisitList == null){
                log.error("customerVisitList(map={}) is not found", map);
                return Response.fail("customerVisitList.not.found");
            }
            return Response.ok(customerVisitList);
        } catch (Exception e) {
            log.error("failed to find customerVisitList(map={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("customerVisitList.find.fail");
        }
    }

    @Override
    public Response<Paging<CustomerVisit>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo = new PageInfo(offset,limit);
            Paging<CustomerVisit> customerVisitPaging = customerVisitDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),map);
            if (customerVisitPaging == null){
                log.error("customerVisitPaging(map={}) is not found", map);
                return Response.fail("customerVisitPaging.not.found");
            }
            return Response.ok(customerVisitPaging);
        } catch (Exception e) {
            log.error("failed to find customerVisitPaging(map={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("customerVisitPaging.find.fail");
        }
    }

    @Override
    public Boolean update(CustomerVisit customerVisit) {
        try {
            Boolean b = customerVisitDao.update(customerVisit);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(CustomerVisit customerVisit) {
        try {
            customerVisitDao.create(customerVisit);
            return customerVisit.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<CustomerVisit> customerVisitList) {
        try {
            return customerVisitDao.creates(customerVisitList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }
}
